package com.service.impl;

import com.bean.Role;
import com.dao.MiddleDao;
import com.dao.RolesDao;
import com.dao.impl.MenuDaoImpl;
import com.dao.impl.MiddleDaoImpl;
import com.dao.impl.RolesDaoImpl;
import com.service.RolesService;

import java.util.List;

public class RolesServiceImpl implements RolesService {
   MiddleDao  middleDao = new MiddleDaoImpl();
    RolesDao rolesDao = new RolesDaoImpl();
    @Override
    public List<Role> getRolesList(int pageIndex, int pageSize) {
        return rolesDao.getRolesList(pageIndex,pageSize);
    }

    @Override
    public int total() {
        return rolesDao.total();
    }

    @Override //事务问题
    public int insertRole(String rolename, String state, String[] ids) {
        int k1 =0;
        try {
            //1.新增角色表
            Role role = new Role();
            role.setRoleName(rolename);
            role.setRoleState(Integer.parseInt(state));
            int k = rolesDao.insert(role);
            //2.新增中间表
            middleDao.insertMiddle(k,ids);
            k1=1;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return k1;
    }

    @Override
    public Role findbyid(int roleId) {
        return rolesDao.findbyid(roleId);
    }

    @Override
    public int updateRole(int roleId, String roleName, int roleState, String[] ids) {
        int k1 =0;
        try {
            //1.删除中间表
            middleDao.delMiddle(roleId);
            //2.新增中间表
            middleDao.insertMiddle(roleId,ids);
            rolesDao.updateRole(roleId,roleName,roleState);
            k1=1;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return k1;

    }

    @Override
    public int delete(int roleId) {
        int k1 = 0;
        try {
            //1.删除中间表
            middleDao.delMiddle(roleId);
            //2.删除角色表
            rolesDao.delete(roleId);
            k1 = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return k1;
    }
}
